/**
 * 功能描述 列表
 */

define(function (require, exports, module){
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	var store =require("store");
	var enmu = require("enums");
	var Form = require("form");
	
	var instance = {
		initComponent:function (params,widget){
			var grid = new Grid({
				parentNode:".J-grid",
				model:{
					url:"api/nonmemgroupreserve/querycharge",
					params : function() {
						var subnav = widget.get("subnav");
						var status = subnav.getValue("status");
						var operateDate = subnav.getValue("operateDate").start;
						var operateDateEnd = subnav.getValue("operateDate").end;
						return {
							"nonMemberGroup.status":status,
							"nonMemberGroup.operateDate":operateDate,
							"nonMemberGroup.operateDateEnd":operateDateEnd,
							"nonMemberGroup.signUpType":"Experience",
							orderString:"nonMemberGroup.checkInDate:desc",
							fetchProperties:"*,nonMemberGroup.*,nonMemberGroup.servicePoint.name," +
        					"nonMemberGroup.servicePoint.pkServicePoint," +
        					"nonMemberGroup.operator.pkUser," +
        					"nonMemberGroup.signUps.version," +
        					"nonMemberGroup.signUps.signUpRegister.pkSignUpRegister," +
        					"nonMemberGroup.signUps.signUpRegister.exitGroup," +
        					"signUpRooms.roomType.*," +
        					"signUpRooms.count" 
						};
					},
					columns:[{
						name: "nonMemberGroup.contactName",
						label: "联系人"
					},{
						name: "nonMemberGroup.contactPhone",
						label: "联系方式",
					},{
						name: "nonMemberGroup.operateDate",
						label: "登记日期",
						format: "date",
						formatparams:{
							mode:"YYYY-MM-DD"
						}
					},{
						name:"nonMemberGroup.servicePoint.name",
						label:"目的地",
					},{
            			name : "roomType",
            			label : "房型",
            			format : function(data, row){
            				if(row.signUpRooms[0]){
            					return row.signUpRooms[0].roomType == null ? "" :row.signUpRooms[0].roomType.name;
            				}else{
            					return "";
            				}
            			}
            		},{
						name:"count",
						label:"体验人数",
						format: function(value,row){
							var signUpNumber = 0,data=row.nonMemberGroup.signUps;
				        	if(data){
				        		for(var i = 0; i<data.length; i++){
				        			if(!data[i].signUpRegister.exitGroup){
				        				signUpNumber++
				        			}
				        		}
				        	}
				        	return signUpNumber;
						}
					},{
						name:"nonMemberGroup.deposit",
						label:"订金",
					},{
						name:"nonMemberGroup.status.value",
						label:"体验状态",
						format: function(value,row){
							if(row.nonMemberGroup.status.key == "Initial"){
								return "待收费"
							}else if(row.nonMemberGroup.status.key == "Effective"){
								return "已收费"
							}else{
								return row.nonMemberGroup.status.value;
							}
						}
					},{
						name:"show",
						label:"操作",
						format:"button",
						formatparams:[{
							id:"edit",
							icon:"icon-edit",
							text:"收费",
							handler:function(index,data,rowEle){
								if(data.nonMemberGroup.status.key=="Effective"){
									Dialog.alert({
				                		title:"提示",
				                		content:"该状态下，不能再收费了！"
				                	});
									return false;
								}else{
									var remarkForm =new Form({
										parentNode:".J-form-dialog",
										autoRender : false,
						        		model:{
											id:"remarkForm",
											defaultButton:false,
											items:[{
												name:"deposit",
												label:"应收(元)",
												readonly:true,
												defaultValue :data.nonMemberGroup.deposit
											},{
												name:"money",
												label:"实收(元)",
												validate : ["required"],
												 events:{
														"blur":function(e){
															var deposit = parseInt(remarkForm.getValue("deposit"));
															var money = parseInt(remarkForm.getValue("money"));
															if(money>deposit){
																Dialog.alert({
																	title:"提示",
																	id:"money",
																	content:"实收金额不能大于应收金额",
																	confirm:function(){
																		remarkForm.setValue("money","")
																		Dialog.close("money");
																		return false;
																	}
																});
															}
														},
											        },
											},{
												name:"paymentMethod",
												label:"收费方式",
												type:"select",
												validate : ["required"],
												options:enmu["com.eling.elcms.travel.model.TravelContractCharge.PaymentMethod"],
											},{
												name:"payDescription",
												type:"textarea",
												label:"备注",
											}]
										 }
									});
									widget.set("remarkForm",remarkForm);
									Dialog.showComponent(remarkForm,{
										defaultButton:false,
										title: "体验订金",
										setStyle: function(){
											$(".el-dialog-modal .modal").css({
												"top": "10%",
												"width":"50%",
												"left": "25%",
											})
										},
										buttons :[{
											id:"save",
											text:"保存",
		 									className:"btn-primary",
											handler:function(){
												if(remarkForm.valid()){
													var FormData = remarkForm.getData();
													var saveData = {};
													saveData.pkNonMemberGroup = data.nonMemberGroup.pkNonMemberGroup;
													saveData.money = FormData.money;
													saveData.paymentMethod = FormData.paymentMethod;
													saveData.payDescription =FormData.payDescription,
													aw.saveOrUpdate("api/nonmembergroup/pay",aw.customParam(saveData),function(saveData){
														Dialog.close();
									          			widget.get("grid").refresh();
									       			});
												};
											}
										},{
											id:"close",
											text:"取消",
											handler:function(){
												Dialog.close();
											}
										}]
									});
								}
							}
						},{
							id:"show",
							text:"查看",
							handler:function(index,data,rowEle){
								var grid = widget.get("optiongrid");
								grid.setTitle("客户信息");
								widget.show([".J-editgrid",".J-form"]).hide([".J-grid"]);
	    						widget.get("subnav").show(["return"]).hide(["search","status","operateDate"]);
	    						aw.ajax({
	    			        		url : "api/nonmembergroup/queryall",
	    			        		data : {
	    			        			pkNonMemberGroup:data.nonMemberGroup.pkNonMemberGroup,
	    			        			fetchProperties:"nonMemberGroup.*," +
	    			        					"nonMemberGroup.paymentMethod.*," +
	    			        					"nonMemberGroup.status.*," +
	    			        					"nonMemberGroup.servicePoint.*," +
	    			        					"signUps.signUpRegister.*," +
	    			        					"signUps.signUpRegister.personalInfo.*," +
	    			        					"signUps.signUpRegister.personalInfo.sex.*," +
	    			        					"signUpRooms.roomType.*," +
	    			        					"signUpRooms.count" 
	    			        		},
	    			        		dataType : "json",
	    			        		success : function(datas){
	    			        			var form = widget.get("form");
	    			        			var formData = datas.nonMemberGroup;
	    			        			var roomType = datas.signUpRooms[0].roomType
	    			        			var roomCount = datas.signUpRooms[0].count
	    			        			var gridData = datas.signUps
	    			        			//给表单赋值
	    			        			form.setValue("servicePoint",formData.servicePoint);
	    			        			form.setValue("roomType",roomType);
	    			        			form.setValue("count",roomCount);
	    			        			form.setValue("checkInDate",formData.checkInDate);
	    			        			form.setValue("checkOutDate",formData.checkOutDate);
	    			        			form.setValue("deposit",formData.deposit);
	    			        			var signUpNumber = 0;
	    			                	if(gridData){
	    			                		for(var i = 0; i<gridData.length; i++){
	    			                			if(!gridData[i].signUpRegister.exitGroup){
	    			                				signUpNumber++
	    			                			}
	    			                		}
	    			                	}
	    			        			form.setValue("signUpNumber",signUpNumber);
	    			        			form.setValue("contactName",formData.contactName);
	    			        			form.setValue("contactPhone",formData.contactPhone);
	    			        			form.setValue("operateDate",formData.operateDate);
	    			        			form.setValue("description",formData.description);
	    			        			form.setValue("status",formData.status.key);
	    			        			form.setValue("money",formData.money);
	    			        			form.setValue("chargeDate",formData.chargeDate);
	    			        			
	    			        			//给列表赋值
	    			        			var dat =[];
	    			        			for(var i = 0; i<gridData.length; i++){
	    			            			if(gridData[i].signUpRegister.exitGroup){
	    			            				delete gridData[i];
	    			            			}
	    			            		};
	    			        			for(var i in gridData){
	    			        				dat.push(gridData[i].signUpRegister);
	    			        			}
	    			        			grid.setData(dat);
	    			        			
	    			        			form.setDisabled(true);
	    			        		}
	    			        	});
							}
						}]
					}]
				}
			});
			widget.set("grid",grid);
		}
	};
	module.exports = instance;
});
